//
//  ZGSOneArticleViewController.m
//  MapleLeaf
//
//  Created by dllo on 15/12/7.
//  Copyright © 2015年 AntennaBabyTeam. All rights reserved.
//

#import "ZGSOneArticleViewController.h"
#import "ZGSOneArticleModel.h"
#import "ZGSMineViewController.h"
#import "ZGSArtleViewController.h"
#import "ZGSQuestionController.h"
@interface ZGSOneArticleViewController ()
@property (nonatomic, retain) ZGSOneArticleModel *article;
@property (nonatomic, retain) UIView *headBgView;
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) NSMutableAttributedString *attributedString;
@property (nonatomic, assign) CGFloat contentHeihgt;
@property (nonatomic, retain) MBProgressHUD *hud;

@end

@implementation ZGSOneArticleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self createData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - createData

- (void)createData {
    
    [self createHeadBgView];
    
    AFNetworkReachabilityManager *nManager = [AFNetworkReachabilityManager sharedManager];
    
    // 开始检测
    [nManager startMonitoring];
    [nManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == 1 || status == 2) {
            NSDate *date = [NSDate date];
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
            formatter.dateFormat = @"yyyy-MM-dd";
            NSString *strDate = [formatter stringFromDate:date];
            NSLog(@"%@", strDate);
            NSDictionary *postDic = @{@"strDate":date, @"strRow":@"1"};
            [BaseAFN POSTURLByString:@"http://rest.wufazhuce.com/OneForWeb/one/getC_N" bodyByDic:postDic AFNData:^(id result) {
                NSDictionary *dic = result[@"contentEntity"];
                
                
                self.article = [[ZGSOneArticleModel alloc] init];
                
                [self.article setValuesForKeysWithDictionary:dic];
                
                
                NSString *str = self.article.strContent;
                
                str = [str stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];
                
                self.attributedString = [[NSMutableAttributedString alloc]initWithString:str];
                
                NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
                
                [paragraphStyle setLineSpacing:5];
                
                [self.attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, str.length)];
                
                [self.attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, str.length)];
                
                UILabel *label = [[UILabel alloc] init];
                
                label.numberOfLines = 0;
                
                label.font = [UIFont systemFontOfSize:17];
                
                label.attributedText = self.attributedString;
                
                CGSize size = CGSizeMake(WIDTH - 30, 30000);
                
                CGSize labelSize = [label sizeThatFits:size];
                
                self.contentHeihgt = labelSize.height;
                                
                
                [self.hud removeFromSuperview];
                                
                
                [self createViews];

                
            }];
            
        }
        else {
            [self.hud removeFromSuperview];
            
            MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
            [self.view addSubview:hud];
            hud.mode = MBProgressHUDModeText;
            hud.labelText = @"数据请求失败,请检查网络后重试";
            [hud showAnimated:YES whileExecutingBlock:^{
                sleep(1);
            } completionBlock:^{
                [hud removeFromSuperview];
            }];
        }
        
    }];
    
    // 停止检测
    [nManager stopMonitoring];
    
}



#pragma mark - createViews

- (void)createHeadBgView {
    self.headBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    self.headBgView.backgroundColor = [UIColor colorWithRed:0.961f green:0.325f blue:0.173f alpha:1.00f];
    [self.view addSubview:self.headBgView];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(10, 27, 35, 25);
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    backButton.tintColor = [UIColor whiteColor];
    backButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headBgView addSubview:backButton];
    
//    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    shareButton.frame = CGRectMake(WIDTH - 40, 25, 25, 25);
//    [shareButton setBackgroundImage:[UIImage imageNamed:@"iconfont-share.png"] forState:UIControlStateNormal];
//    [shareButton addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchDragInside];
//    [self.headBgView addSubview:shareButton];
    
    UIButton *sentenceButton = [UIButton buttonWithType:UIButtonTypeSystem];
    sentenceButton.frame = CGRectMake(55, 27, 50, 25);
    [sentenceButton setTitle:@"句子" forState:UIControlStateNormal];
    //    sentenceButton.backgroundColor = [UIColor whiteColor];
    sentenceButton.tintColor = [UIColor whiteColor];
    //    sentenceButton.tintColor = [UIColor colorWithRed:0.961f green:0.325f blue:0.173f alpha:1.00f];
    //    sentenceButton.layer.cornerRadius = 13;
    sentenceButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
    [sentenceButton addTarget:self action:@selector(changePageAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headBgView addSubview:sentenceButton];
    
    UIButton *acticleButton = [UIButton buttonWithType:UIButtonTypeSystem];
    acticleButton.frame = CGRectMake(110, 27, 50, 25);
    [acticleButton setTitle:@"文章" forState:UIControlStateNormal];
    acticleButton.tintColor = [UIColor colorWithRed:0.961f green:0.325f blue:0.173f alpha:1.00f];
    acticleButton.backgroundColor = [UIColor whiteColor];
    acticleButton.layer.cornerRadius = 13;
    acticleButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
    [self.headBgView addSubview:acticleButton];
    
    UIButton *questionButton = [UIButton buttonWithType:UIButtonTypeSystem];
    questionButton.frame = CGRectMake(165, 27, 50, 25);
    [questionButton setTitle:@"问题" forState:UIControlStateNormal];
    questionButton.tintColor = [UIColor whiteColor];
    //    questionButton.backgroundColor = [UIColor whiteColor];
    //    questionButton.layer.cornerRadius = 13;
    questionButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
    [questionButton addTarget:self action:@selector(changePageAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headBgView addSubview:questionButton];
}

- (void)createViews {
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64 - 49)];
    [self.view addSubview:self.scrollView];
    
    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 150, 15)];
    dateLabel.font = [UIFont fontWithName:@"ArialUnicodeMS" size:15];
    NSString *dayStr = [self.article.strContMarketTime substringFromIndex:self.article.strContMarketTime.length - 2];
    
    NSString *monthStr = [self.article.strContMarketTime substringWithRange:NSMakeRange(self.article.strContMarketTime.length - 5, 2)];
    
    NSString *yearStr = [self.article.strContMarketTime substringToIndex:4];
    
    NSInteger month = [monthStr integerValue];
    switch (month) {
        case 1:
            dateLabel.text = [NSString stringWithFormat:@"January %@,%@", dayStr, yearStr];
            break;
        case 2:
            dateLabel.text = [NSString stringWithFormat:@"February %@,%@", dayStr, yearStr];
            break;
        case 3:
            dateLabel.text = [NSString stringWithFormat:@"March %@,%@", dayStr, yearStr];
            break;
        case 4:
            dateLabel.text = [NSString stringWithFormat:@"April %@,%@", dayStr, yearStr];
            break;
        case 5:
            dateLabel.text = [NSString stringWithFormat:@"May %@,%@", dayStr, yearStr];
            break;
        case 6:
            dateLabel.text = [NSString stringWithFormat:@"June %@,%@", dayStr, yearStr];
            break;
        case 7:
            dateLabel.text = [NSString stringWithFormat:@"July %@,%@", dayStr, yearStr];
            break;
        case 8:
            dateLabel.text = [NSString stringWithFormat:@"August %@,%@", dayStr, yearStr];
            break;
        case 9:
            dateLabel.text = [NSString stringWithFormat:@"September %@,%@", dayStr, yearStr];
            break;
        case 10:
            dateLabel.text = [NSString stringWithFormat:@"October %@,%@", dayStr, yearStr];
            break;
        case 11:
            dateLabel.text = [NSString stringWithFormat:@"November %@,%@", dayStr, yearStr];
            break;
        case 12:
            dateLabel.text = [NSString stringWithFormat:@"December %@,%@", dayStr, yearStr];
            break;
            
        default:
            break;
    }
    [self.scrollView addSubview:dateLabel];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 55, 250, 25)];
    titleLabel.font = [UIFont boldSystemFontOfSize:21.0];
    titleLabel.text = self.article.strContTitle;
    [self.scrollView addSubview:titleLabel];
    
    UILabel *authorLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 85, 100, 20)];
    authorLabel.textColor = [UIColor colorWithRed:0.725f green:0.729f blue:0.733f alpha:1.00f];
    authorLabel.font = [UIFont boldSystemFontOfSize:18.0];
    authorLabel.text = self.article.strContAuthor;
    [self.scrollView addSubview:authorLabel];
    
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 120, WIDTH - 30, 8100)];
    //    contentLabel.font = [UIFont systemFontOfSize:17];
    contentLabel.backgroundColor = [UIColor whiteColor];
    contentLabel.numberOfLines = 0;
    contentLabel.attributedText = self.attributedString;
    //    [contentLabel sizeToFit];
    self.scrollView.contentSize = CGSizeMake(0, 150 + contentLabel.frame.size.height);
    [self.scrollView addSubview:contentLabel];
}
#pragma mark - ButtonAction

- (void)backAction:(UIButton *)button {
    
    CATransition *transition = [CATransition animation];
    transition.duration = 1.0f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = @"cube";
    transition.subtype = kCATransitionFromLeft;
    transition.delegate = self;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    
    ZGSMineViewController *userVC = [[ZGSMineViewController alloc] init];
    [self.navigationController pushViewController:userVC animated:YES];
}

- (void)changePageAction:(UIButton *)button {
    if ([button.titleLabel.text isEqualToString:@"句子"]) {
        CATransition *transition = [CATransition animation];
        transition.duration = 1.0f;
        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        transition.type = @"pageCurl";
        transition.subtype = kCATransitionFromLeft;
        transition.delegate = self;
        [self.navigationController.view.layer addAnimation:transition forKey:nil];
        
        ZGSArtleViewController *ocVC = [[ZGSArtleViewController alloc] init];
        [self.navigationController pushViewController:ocVC animated:YES];
    }
    else if ([button.titleLabel.text isEqualToString:@"问题"]) {
        CATransition *transition = [CATransition animation];
        transition.duration = 1.0f;
        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        transition.type = @"pageCurl";
        transition.subtype = kCATransitionFromRight;
        transition.delegate = self;
        [self.navigationController.view.layer addAnimation:transition forKey:nil];
        
        ZGSQuestionController *oqVC = [[ZGSQuestionController alloc] init];
        [self.navigationController pushViewController:oqVC animated:YES];
    }
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
